package removeDuplicates;

import java.util.Stack;

public class Solution {
    public String removeDuplicates(String s) {
        // 删除两个相邻的字符
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if(!stack.isEmpty()&&stack.peek()==s.charAt(i)) {
                // 消除
                stack.pop();
            } else {
                stack.push(s.charAt(i));
            }
        }
        String ans = new String();
        while (!stack.isEmpty()) {
            ans = stack.pop()+ans;
        }
        return ans;
    }
}
